package org.yangspring.samples.mvc.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domain.AnimalJoin;
import org.yangspring.samples.mvc.domain.Company;

import java.util.List;

/**
 * Created by whyang on 2017/06/19.
 */

@Repository
public class CompanyDAOImpl implements CompanyDAO {

    private static final Logger logger = LoggerFactory.getLogger(BooksDaoImpl.class);


    //    private LocalSessionFactoryBean sessionFactory;
    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryMysql")
    private SessionFactory sessionFactory;
    /*public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/
    /*public SessionFactory getSessionFactory(){
        return this.sessionFactory;
    }*/

    public void addCompany(Company company){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        session.persist(company);
        logger.info("Company saved successfully, Company Details=" + company);
    }

    public void updateCompany(Company company){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        session.update(company);
        logger.info("Company updated successfully, Company Details=" + company);
    }

    public List<Company> listCompany(){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        List<Company> companyList = session.createQuery("from Company").list();
        for(Company b : companyList){
            logger.info("Company List::" + b);
        }
        return companyList;
//        return new ArrayList<Company>();
    }

    public Company getCompanyById(int id){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        Company c = (Company) session.load(AnimalJoin.class, new Integer(id));
        logger.info("Company loaded successfully, Company details=" + c);
        return c;
//        return new Company();
    }

    public void removeCompany(int id){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        Company c = (Company) session.load(Company.class, new Integer(id));
        if(null != c){
            session.delete(c);
        }
        logger.info("Company deleted successfully, Company details=" + c);
    }

}
